package com.tuling.quartz;

import org.quartz.*;

import java.util.Date;

@DisallowConcurrentExecution    // 禁止并发执行同一个Job定义的多个实例
@PersistJobDataAfterExecution   // 持久化JobDetail中的JobDataMap(对Trigger中无效)
public class MyJob implements Job {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
//        JobDataMap jobDetailMap = context.getJobDetail().getJobDataMap();
//        JobDataMap triggerMap = context.getTrigger().getJobDataMap();
//        // 将上面两个汇总成一个Map
//        JobDataMap mergeMap = context.getMergedJobDataMap();
//        System.out.println("jobDetailMap：" + jobDetailMap.getString("job"));
//        System.out.println("triggerMap：" + triggerMap.getString("trigger"));
//        System.out.println("mergeMap：" + mergeMap.getString("trigger"));
//        // 如果JobDetail和Trigger都设置了name，那么会被覆盖
//        System.out.println("name：" + this.name);

//        // 每次执行，都是不同的实例
//        System.out.println("jobDetail：" + System.identityHashCode(context.getJobDetail()));
//        System.out.println("job：" + System.identityHashCode(context.getJobInstance()));

//        System.out.println("execute：" + new Date());
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        JobDataMap jobDetailMap = context.getJobDetail().getJobDataMap();
        JobDataMap triggerMap = context.getTrigger().getJobDataMap();
        jobDetailMap.put("count1", jobDetailMap.getInt("count1") + 1);
        triggerMap.put("count2", triggerMap.getInt("count2") + 1);
        System.out.println("jobDetailMap count：" + jobDetailMap.getInt("count1"));
        System.out.println("triggerMap count：" + triggerMap.getInt("count2"));
    }
}
